import 'package:flutter/material.dart';

import '../models/LoginResponseData.dart';
import '../utils/connections.dart';

///登录页
///江志武、胡泽新创建
///孙焰明实现登录功能
class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  String _count, _passWords;
  bool _isObscure = true;
  TextEditingController _nameController = TextEditingController();
  TextEditingController _pwdController = TextEditingController();
  GlobalKey _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("登录"),
      ),
      body: Padding(
        padding: EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0),
        child: Form(
          key: _formKey,
          child: Column(
            children: [
              TextFormField(
                controller: _nameController,
                autofocus: true,
                decoration: InputDecoration(
                  labelText: '用户',
                  hintText: '邮箱',
                  prefixIcon: Icon(Icons.person),
                ),
                validator: (String v) {
                  var countRex = RegExp(
                      r"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?");
                  if (!countRex.hasMatch(v)) {
                    return "请输入正确的账户";
                  }
                  return null;
                },
                onSaved: (String v) => _count = v,
              ),
              TextFormField(
                controller: _pwdController,
                obscureText: true,
                decoration: InputDecoration(
                  labelText: '密码',
                  hintText: '您的登录密码',
                  prefixIcon: Icon(Icons.lock),
                ),
                validator: (v) => v.trim().length > 0 ? null : '密码不能为空',
              ),
              Padding(
                padding: EdgeInsets.only(top: 28.0),
                child: Row(
                  children: [
                    Expanded(
                      child: ElevatedButton(
                        child: Text('登录'),
                        onPressed: () async {
                          if ((_formKey.currentState as FormState).validate()) {
                            LoginResponseData res = await login(
                                _nameController.text, _pwdController.text);
                            print(res);
                            // todo
                            if (res?.status == "success") {
                              Navigator.pushNamed(context, "index", arguments: [
                                res.data.name,
                                res.token,
                                res.data.user_id
                              ]);
                            } else {
                              showDialog(
                                  context: context,
                                  builder: (context) {
                                    return AlertDialog(
                                      title: Text("提示"),
                                      content: Text("用户邮箱或密码错误！"),
                                      backgroundColor: Colors.grey,
                                      shape: RoundedRectangleBorder(
                                          borderRadius:
                                              BorderRadius.circular(20)),
                                      actions: [
                                        TextButton(
                                          onPressed: () =>
                                              Navigator.pop(context),
                                          child: Text("确认"),
                                        ),
                                      ],
                                    );
                                  });
                              // Navigator.pushNamed(context, "index",
                              //     arguments: res);
                            }
                          }
                        },
                        style: ElevatedButton.styleFrom(
                          padding: EdgeInsets.all(15.0),
                          onSurface: Theme.of(context).primaryColor,
                          onPrimary: Colors.white,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
